package com.joymart.admin.rest;


import com.joymart.aftersale.service.AfterSaleQueryService;
import com.joymart.aftersale.service.AfterSaleService;
import com.joymart.common.model.ApprovalStatus;
import com.joymart.common.model.Range;
import com.joymart.common.utils.SecurityUtils;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin")
public class AfterSaleAdminController {

    @Autowired
    private AfterSaleService afterSaleService;
    @Autowired
    private AfterSaleQueryService afterSaleQueryService;

    @GetMapping("/afterSales")
    public Page<?> searchWaitingAfterSales( @RequestParam LocalDateTime start,
                                            @RequestParam LocalDateTime end,
                                            @RequestParam(value = "targetStatus", required = false) ApprovalStatus approvalStatus,
                                            @RequestParam(defaultValue = "", required = false) String orderId,
                                            @RequestParam(defaultValue = "false", required = false) boolean waitingOnly,
                                            @RequestParam(value = "pageNo", defaultValue = "0")Integer pageNo,
                                            @RequestParam(value = "pageSize", defaultValue = "20")Integer pageSize) {
        Range range = Range.of(start, end);
        Pageable pageable = PageRequest.of(pageNo, pageSize);
        return afterSaleQueryService.searchAdminAfterSales(range, pageable, approvalStatus, orderId, waitingOnly);
    }

    @PutMapping(value = "/afterSales/{id}" ,params = "action=CONFIRM_HEAD_QUARTER_REFUNDED")
    public void confirmHeadQuarterRefunded(@PathVariable String id) {
        SecurityUtils.requiresRestAuthority("OrderRelated");
        afterSaleService.confirmHeadQuarterRefunded(id);
    }


    @GetMapping(value = "/afterSales/{id}")
    public AfterSaleQueryService.AfterSaleSheetDto getAfterSalesDetails(@PathVariable String id) {
        val afterSaleSheet = afterSaleService.findById(id);
        return afterSaleQueryService.toDto(afterSaleSheet);
    }




}
